package eclipse.util;

import eclipse.ErrorManager;

/**
 *  A basic thread interface that has CPU limitations.
 * Interval represents how many ms delay between executions
 * setRunStatus(false); will kill the thread
 * setEnabled(true); is required for running.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public abstract class ThreadedTimer extends Thread
{
	//ms for the code to be executed
	private long 	Interval	= 1000;
	//specifies if the code is to be executed
	private boolean Enabled		= false;
	//specifies if the thread is running
	private boolean isRunning 	= false;
	/**
	 * Sets how frequent the thread is executed
	 * @param lInterval Frequency in milliseconds
	 */
	public void setInterval(long lInterval)
	{
		this.Interval = lInterval;
	}
	/**
	 * Returns the frequency of execution
	 * @return
	 */
	public long getInterval()
	{
		return this.Interval;
	}
	/**
	 * Enables the execution of the Related code
	 * @param bEnabled
	 */
	public void setEnabled(boolean Enabled)
	{
		this.Enabled = Enabled;
	}
	/**
	 * Returns if the Timer is enabled.
	 */
	public boolean getEnabled()
	{
		return this.Enabled;
	}
	/**
	 * Sets the run status of the Thread.
	 * false = Shuts down the thread
	 */
	public void setRunStatus(boolean bStatus)
	{
		this.isRunning = bStatus;
	}
	/**
	 * Returns the run status of the Thread.
	 * Same as isAlive();
	 */
	public boolean getRunStatus()
	{
		return this.isRunning;
	}
	/**
	 * Creates a ThreadedTimer object
	 */
	public ThreadedTimer()
	{
		//make it so that the Thread is running
		this.isRunning 	= true;
	}
	/**
	 * Creates a ThreadedTimer object
	 * @param iInterval Frequency of execution
	 */
	public ThreadedTimer(int Interval)
	{
		this.Interval 	= Interval;
		this.isRunning 	= true;		
	}
	public void run()
	{
		//Continue running while needed
		while(isRunning)
		{
			//only execute when enabled
			if(Enabled)
			{
				//execute the function
				tick(); 
			}
			//wait for the specified seconds
			Sleep(Interval);
		}
		//state when the thread ends
		System.out.println("Ending Thread: " + getName());
	}
	/**
	 * Pauses the thread
	 * @param ms Miliseconds for pause
	 */
	public void Sleep(long ms)
	{
		//try to sleep the thread
		try 
		{
			//wait for x miliseconds
			sleep(ms);
		}
		catch (InterruptedException e) 
		{
			//handle the error if it happens
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Hardcoded to run as timed.	 
	 */
	public abstract void tick();
}
